﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using myu.Data;

namespace Sar
{
    public partial class FormLogin : Form
    {
        #region ctor
        public FormLogin()
        {
            InitializeComponent();
        }
        #endregion


        #region field
        public Int32 Previlege { get; set; }
        #endregion


        #region method
        private void buttonOK_Click(object sender, EventArgs e)
        {
            using (SqlDataOpr opr = new SqlDataOpr(
                Convert.ToString("EXECUTE dbo.Login '" + textBoxUsername.Text + "', '" + textBoxPassword.Text + "'")))
            {
                using (SqlDataReader reader = opr.fetchReader())
                {
                    if (!reader.HasRows)
                    {
                        MessageBox.Show("Error: 账号或密码错误", "温馨提示");
                        return;
                    }
                    reader.Read();
                    this.Previlege = Convert.ToInt32(reader["AccountPrevilege"]);
                    this.DialogResult = DialogResult.OK;
                    this.Dispose();
                }
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Dispose();
        }

        private void textBoxUsername_Enter(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (textBox.ForeColor == Color.Gray)
            {
                textBox.Text = null;
                textBox.ForeColor = Color.Black;
            }
        }

        private void textBoxUsername_Leave(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (String.IsNullOrEmpty(textBox.Text))
            {
                textBox.Text = "Username";
                textBox.ForeColor = Color.Gray;
                textBox.Font = new Font("宋体", 11F, System.Drawing.FontStyle.Italic);
            }
        }

        private void textBoxPassword_Enter(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (textBox.ForeColor == Color.Gray)
            {
                textBox.Text = null;
                textBox.PasswordChar = '*';
                //textBox.PasswordChar = '●';
                textBox.ForeColor = Color.Black;
                textBox.Font = new Font("宋体", 11F, System.Drawing.FontStyle.Regular);
            }
        }

        private void textBoxPassword_Leave(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (String.IsNullOrEmpty(textBox.Text))
            {
                textBox.Text = "Password";
                textBox.PasswordChar = '\0';
                textBox.ForeColor = Color.Gray;
                textBox.Font = new Font("宋体", 11F, System.Drawing.FontStyle.Italic);
            }
        }
        #endregion
    }
}
